<template>
	<view class="previewLayout sysBg">
		<swiper circular :current="currentIndex" @change="swiperChange">
			<swiper-item v-for="(item,index) in imageList" :key="item">
				<image @click="switchMask" :src="item" mode="aspectFit" />
			</swiper-item>
		</swiper>
		<view class="mask" @click="switchMask" v-if="maskState">
			<view class="back" @click.stop="goPrev" :style="{marginTop:getStatusBarHeight()+'px',marginLeft:'40rpx'}">
				<uni-icons type="left" color="white" size="24"></uni-icons>
			</view>
			<view class="content">
				<view class="datetime">
					<view class="count">3/8</view>
					<view class="time">
						<uni-dateformat :date="new Date()" format="hh:mm"></uni-dateformat>
					</view>
					<view class="date">
						<uni-dateformat :date="new Date()" format="MM月dd日"></uni-dateformat>
					</view>
				</view>
				<view class="scorebar">
					<view class="detail" @click.stop="popSheet">
						<view>
							<uni-icons size="30" type="info"></uni-icons>
						</view>
						<view class="text">信息</view>
					</view>
					<view class="detail" @click.stop="openUserScore">
						<view>
							<uni-icons size="30" type="star"></uni-icons>
						</view>
						<view class="text">5分</view>
					</view>
					<view class="detail" @click.stop="downFile">
						<view>
							<uni-icons size="30" type="download"></uni-icons>
						</view>
						<view class="text">下载</view>
					</view>
				</view>
			</view>
		</view>
		<PopupComponent ref="popupComponent">
			<template #popcontent>
				<view class="popview">
					<view class="title">
						<view></view>
						<view>机车介绍</view>
						<view class="close" @click="closePopupLayer">
							<uni-icons type="closeempty"></uni-icons>
						</view>
					</view>
					<scroll-view scroll-y="true">
						<view class="content">
							<view class="row">
								<view class="label">壁纸ID:</view>
								<text class="value" selectable>987ab300k23ea002234324</text>
							</view>
							<view class="row">
								<view class="label">发布者:</view>
								<text class="value" selectable>摩托范</text>
							</view>
							<view class="row">
								<view class="label">评分:</view>
								<view class="value" selectable>
									<view class="userScore">
										<uni-rate v-model="score" @change="onChange"></uni-rate>
										<text class="score">{{score}}</text>
									</view>
								</view>
							</view>
							<view class="row">
								<view class="label">摘要:</view>
								<view class="value">
									搭载853cc直列双缸水冷发动机，最大功率57kW（77马力），最大扭矩86-87N·m。中低转速（3000-6000转）扭矩充沛，适合非铺装路面脱困，但低转速（如市区跟车时）略显拖沓，需频繁降档
								</view>
							</view>
							<view class="row">
								<view class="label">标签:</view>
								<view class="value">
									<view class="userlabel">
										<view class="labelContent" :key="item" v-for="item in ['设计时尚','动力强劲','驾驶轻巧']">
											{{item}}
										</view>
									</view>
								</view>
							</view>
						</view>
						<view class="copyright">
							声明:本图片来用户投稿，非商业使用，用于免费学习交流，如侵犯了您的权益，您可以拷贝壁纸ID举报至平台，邮箱19597923@qq.com，管理将删除侵权壁纸维护您的权益。
						</view>
						<view class="safe-area-inset-bottom"></view>
					</scroll-view>
				</view>
			</template>
		</PopupComponent>
		<ScorePopupComponent ref="scoreComponent">
			<template #popcontent>
				<view class="popScoreView">
					<view class="title">
						<view></view>
						<view>宝马摩托车评分</view>
						<view class="close" @click="closeScorePopupLayer">
							<uni-icons type="closeempty" size="20"></uni-icons>
						</view>
					</view>
					<view class="content">
						<view class="score">
							<uni-rate v-model="userScore" allow-half="true" @change="setChangeScore"></uni-rate>
							<text>{{userScore}}分</text>
						</view>
						<view class="confirm">
							确认评分
						</view>
					</view>
				</view>
			</template>
		</ScorePopupComponent>
	</view>

</template>

<script setup>
	import {
		getStatusBarHeight
	} from "@/utils/system";

	const imageList = ref([
		"https://res2.vmallres.com/pimages/uomcdn/CN/pms/202409/gbom/6942103132216/800_800_9BAD3BCC24D62F134982BB23E027583Bmp.png",
		"https://res.vmallres.com/pimages/uomcdn/CN/pms/202404/gbom/6942103119712/428_428_DF336B32F22094ACC184AEE82E1A5292mp.png",
		"https://res.vmallres.com/pimages/uomcdn/CN/pms/202404/gbom/6942103119088/428_428_42EC40C74080E98F8B3E0799F6602712mp.png",
		"https://res.vmallres.com/pimages/uomcdn/CN/pms/202404/gbom/6942103120374/428_428_606AD050130CDD9F17DBBB7EECDD9B4Amp.png",
		"https://res.vmallres.com/pimages/uomcdn/CN/pms/202402/gbom/6942103116261/428_428_13C707C7E44C89BAC152465D56B3417Emp.png",
		"https://res.vmallres.com/pimages/uomcdn/CN/pms/202402/gbom/6942103115561/428_428_09F9F92F322A2E2D179759082DE19E9Dmp.png",
		"https://res.vmallres.com/pimages/uomcdn/CN/pms/202404/gbom/6942103121081/428_428_E01463FFB1D9208814652C56045C5234mp.png",
		"https://res.vmallres.com/pimages/uomcdn/CN/cms/2024-02/81b9a0c3807b407fa8cc22688ddd71b1.jpg",
		"https://res.vmallres.com/pimages/uomcdn/CN/cms/2024-03/867eea5f98e54745a49c4d61fb524d07.jpg",
		"https://res.vmallres.com/pimages/uomcdn/CN/cms/2024-04/5ac0c8a797f04cd8bd93ca84c11f10e0.jpg",
		"https://res.vmallres.com/pimages/uomcdn/CN/pms/202402/gbom/6942103116261/428_428_13C707C7E44C89BAC152465D56B3417Emp.png",
		'https://res.vmallres.com/pimages/uomcdn/CN/pms/202405/gbom/6942103123054/428_428_6D82420B22F63C056540DF546132E204mp.png',
		"https://res.vmallres.com/pimages/uomcdn/CN/pms/202309/gbom/6942103103650/428_428_16BB4592008F272F5CD34F46BA157EDFmp.png",
		"https://res.vmallres.com/pimages/uomcdn/CN/pms/202401/gbom/6942103109621/428_428_ED696F8D02F436A23883038D9AF06564mp.png",
		"https://res.vmallres.com/pimages/uomcdn/CN/pms/202404/gbom/6942103123634/428_428_252E5BC4769E13DDF06DC495C419593Fmp.png",
		"https://res.vmallres.com/pimages/uomcdn/CN/pms/202405/displayProduct/10086459091537/800_800_b_mobile5E6456714F6DFD17FC02A1817692D9A2.jpg",
		"https://res.vmallres.com/pimages/uomcdn/CN/pms/202305/gbom/6942103106477/428_428_5D9C341CC3FF67A58CCB21DB511E49FEmp.png",
		"https://res.vmallres.com/pimages/uomcdn/CN/pms/202307/gbom/6942103106606/428_428_848391113A410132F1060FE8B1F026D0mp.png",
		"https://res.vmallres.com/pimages/uomcdn/CN/pms/202312/gbom/6942103116933/428_428_1E182F0FD1AC7D8BE3AEB4F29C78802Bmp.png",
		"https://res.vmallres.com/pimages/uomcdn/CN/pms/202311/gbom/6942103115295/428_428_958BC5600021FA54FCF0D7FE29030601mp.png",
		"https://res.vmallres.com/pimages/uomcdn/CN/pms/202403/gbom/6942103117480/428_428_3B943B45EB5100A5B792D5E1F9FD6576mp.png",
		"https://res.vmallres.com/pimages/uomcdn/CN/pms/202401/gbom/6942103114281/428_428_DA0AAB28B567B5F2A70F666B6DBDD67Amp.png",
		"https://res.vmallres.com/pimages/uomcdn/CN/pms/202312/gbom/6942103103780/428_428_ADD429C1243111A5D458BCD57C03C953mp.png",
		"https://res.vmallres.com/pimages/uomcdn/CN/pms/202311/gbom/6941487289370/428_428_473CEB56E31988F4C000C2A04BC206D8mp.png"
	]);


	const currentIndex = ref(0);
	const swiperChange = (e) => {
		console.log(e);
		currentIndex.value = e.detail.current;
	}


	// 创建响应式数据  
	const maskState = ref(true);
	const switchMask = () => {
		maskState.value = !maskState.value;
	}
	const popupComponent = ref(null);
	const scoreComponent = ref(null); //评分组件

	//弹出层
	const popSheet = () => {
		//调用子组件的方法
		popupComponent.value.popupLayer();
	}

	//关闭弹窗
	const closePopupLayer = () => {
		popupComponent.value.closePopupLayer();
	}

	const userScore = ref(0);
	const openUserScore = () => {
		scoreComponent.value.popupLayer();
	}
	const closeScorePopupLayer = () => {
		scoreComponent.value.closePopupLayer();
	}
	const setChangeScore = (e) => {
		userScore.value = e.value;
	}

	//
	const score = ref(4);
	const onChange = (e) => {
		score.value = e.value;
	}

	const goPrev = () => {
		uni.navigateBack({
			success:() =>{
				
			},
			fail: (err) => {
				uni.reLaunch({
					url:"/pages/index/index"
				})
			}
		});
	}


	//下载图片到相册里面
	const downFile = () => {
		//因为H5中不能操作相册，通过条件编译来判断
		// #ifdef H5
		uni.showModal({
			content: "请长安保存图片",
			showCancel: false
		})
		// #endif

		//不是H5的环境
		uni.getImageInfo({
			src: imageList.value[currentIndex.value], //图片地址
			success: (res) => {
				const {
					path = ''
				} = res;
				uni.showLoading({
					title:"下载中.....",
					icon:"none"
				})
				uni.saveImageToPhotosAlbum({
					filePath: path,
					success: (result) => {
						console.log('result====', result);
						if (result.errMsg === "saveImageToPhotosAlbum:ok") {
							uni.showToast({
								title: "保存成功，请到相册里进行查看",
								icon: 'none'
							})
						}

					},
					fail: (err) => {

						//如果用户授权了，但没有保存到相册，会返回这个错误
						if (err.errMsg === "saveImageToPhotosAlbum:fail cancel") {
							uni.showToast({
								title: "保存失败，请重新保存",
								icon: "none"
							});
							return;
						}
						uni.showModal({
							title: "提示",
							content: "需要授权保存相册",
							success: (res) => {
								if (res.confirm) {
									//打开用户授权对框框
									uni.openSetting({
										success: (setting) => {
											//如果授权成功
											if (setting.authSetting[
													"scope.writePhotosAlbum"
													]) {
												uni.showToast({
													title: "获取授权成功",
													icon: "none"
												})
											} else {
												uni.showToast({
													title: "授权失败",
													icon: "none"
												})
											}


										}

									})
								}
							},
							fail: (err) => {
								uni.showToast({
									content: "用户没有授权"
								})
							}
						})
					},
					complete:()=>{
						uni.hideLoading();
					}
				})
			}
		});

		// #ifndef H5

		// #endif
	}
	
	onLoad((e)=>{
		console.log('接受到的参数===',e);
	})
	
	
	
	//分享给好友
	onShareAppMessage((e)=>{
		return {
			title:"华为商城@@",
			path:"/pages/preview/preview?id=1&type=shareFriend",
			
		}
	});
	
	
	//分享到朋友圈函数
	onShareTimeline(()=>{
		return {
			title:"华为商城@@",
			imageUrl:"https://res.vmallres.com/portal/1.25.4.300/h5/images/logo_app.png",
			path:"/pages/preview/preview",
			query:"id=11&type=shareTimeline"
		}
	})
</script>

<style lang="scss">
	.previewLayout {
		position: relative;
		width: 100%;
		height: 100vh;

		swiper {
			width: 100%;
			height: 100%;

			&-item {
				image {
					width: 100%;
					height: 100%;
				}
			}
		}

		.popScoreView {
			background: #fff;
			color: black;
			width: 500rpx;
			border-radius: 30rpx;

			.title {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 20rpx;

			}

			.content {
				display: flex;
				flex-direction: column;
				align-items: center;
				padding: 10rpx 40rpx 40rpx;

				.score {
					display: flex;
				}

				.confirm {
					margin-top: 20rpx;
					height: 60rpx;
					border: 1px solid gray;
					border-radius: 10rpx;
					display: flex;
					align-items: center;
					padding: 20rpx 50rpx;
				}
			}

		}

		.mask {
			position: absolute;
			top: 0;
			left: 0;
			right: 0;
			bottom: 0;
			// background: orange;
			display: flex;
			flex-direction: column;

			.back {
				width: 70rpx;
				height: 70rpx;
				border-radius: 50%;
				background: #000;
				display: flex;
				align-items: center;
				justify-content: center;
				//margin: 60rpx 0 0 30rpx;
				padding: 20rpx;
			}

			.content {
				flex: 1;
				// background: red;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				align-items: center;

				.datetime {
					display: flex;
					flex-direction: column;
					align-items: center;
					// background-color: orange;
					//color: white;
					color: #000;
					margin-top: 30rpx;

					.time {
						font-size: 160rpx;
					}

					.count {
						padding: 5rpx 30rpx;
						background: #986e54;
						backdrop-filter: blur(10rpx);
						border-radius: 20rpx;

					}
				}

				.scorebar {
					display: flex;
					justify-content: space-between;
					align-items: center;
					width: 60%;
					background: #f4ede7;
					padding: 10rpx 60rpx;
					border-radius: 100rpx;
					margin-bottom: 60rpx;

					.detail {
						.text {
							color: #a39c97;
						}
					}

				}
			}


		}

		.popview {
			background: #fff;
			color: black;
			width: 100%;
			border-radius: 30rpx 30rpx 0 0;

			.title {
				width: 100%;
				display: flex;
				justify-content: space-between;
				padding: 20rpx;
				//background: red;
			}

			scroll-view {
				max-height: 70vh;
				overflow: hidden;

				.content {
					.row {
						display: flex;
						align-items: center;
						padding: 24rpx 0;
						font-size: 32rpx;

						.label {
							color: $text-font-color-3;
							width: 140rpx;
							text-align: right;
							font-size: 30rpx;
						}

						.value {
							flex: 1;
							padding: 0 0 0 20rpx;

							.userlabel {
								display: flex;
								flex-wrap: wrap;
								margin-left: 10rpx;

								.labelContent {
									border: 1px solid green;
									border-radius: 10rpx;
									padding: 5rpx 10rpx;
								}

								.labelContent:not(:nth-child(1)) {
									margin-left: 10rpx;
								}
							}

							.userScore {
								display: flex;

								.score {
									margin-left: 10rpx;
								}
							}

						}
					}

				}

				.copyright {
					margin: 20rpx 30rpx;
					padding: 10rpx 20rpx;
					background: $text-font-color-3;
					border-radius: 10rpx;
				}
			}

		}

	}
</style>